package com.wjw.rabbitdemo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test {
    public static Lock lock=new ReentrantLock();
    public static Condition c1=lock.newCondition();
    public static Condition c2=lock.newCondition();
    public static Condition c3=lock.newCondition();
    public static int num=1;
    static class myPrintA extends Thread {
        @Override
        public void run() {
            try {
                lock.lock();

                    while (num != 1){
                        c1.await();
                    }
                    System.out.println("a");
                    num=2;
                    c2.signal();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    static class myPrintB extends Thread {
        @Override
        public void run() {
            try {
                lock.lock();
                    while (num!=2){
                        c2.await();
                    }
                    System.out.println("b");
                    num=3;
                    c3.signal();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    static class myPrintC extends Thread {
        @Override
        public void run() {
            try {
                lock.lock();

                    while (num!= 3){
                        c3.await();// C释放lock锁
                    }

                    System.out.println("c");
                    num=1;
                    c1.signal();// C执行完唤醒A线程

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        while (true){
            new myPrintA().start();
            new myPrintB().start();
            new myPrintC().start();
        }
    }

}
